package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.Collect;
import com.ruoyi.system.domain.Likes;
import com.ruoyi.system.mapper.LikesMapper;
import com.ruoyi.system.service.ILikesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户点赞Service业务层处理
 *
 * @author AousVolad
 * @date 2024-07-05
 */
@Service
public class LikesServiceImpl implements ILikesService {
    private static final Logger log = LoggerFactory.getLogger(LikesServiceImpl.class);
    @Autowired
    private LikesMapper likesMapper;

    /**
     * 查询用户点赞
     *
     * @param id 用户点赞主键
     * @return 用户点赞
     */
    @Override
    public Likes selectLikesById(Long id) {
        return likesMapper.selectLikesById(id);
    }

    /**
     * 查询用户点赞列表
     *
     * @param likes 用户点赞
     * @return 用户点赞
     */
    @Override
    public List<Likes> selectLikesList(Likes likes) {
        return likesMapper.selectLikesList(likes);
    }

    /**
     * 新增用户点赞
     *
     * @param likes 用户点赞
     * @return 结果
     */
    @Override
    public int insertLikes(Likes likes1) {

        //  查询收藏记录 没查到则直接添加，查到则1->0  0->1
        Likes likes = likesMapper.selectLike(likes1);
        System.err.println(likes);

        if (likes != null && likes.getDeletedFlag().equals("0")) {
            likes.setDeletedFlag("1");
            return likesMapper.updateLikes(likes);

        } else if (likes != null && likes.getDeletedFlag().equals("1")) {
            likes.setDeletedFlag("0");
            return likesMapper.updateLikes(likes);
        }

        likes1.setDeletedFlag("0");
        return likesMapper.insertLikes(likes1);
    }

    /**
     * 修改用户点赞
     *
     * @param likes 用户点赞
     * @return 结果
     */
    @Override
    public int updateLikes(Likes likes) {
        return likesMapper.updateLikes(likes);
    }

    /**
     * 批量删除用户点赞
     *
     * @param ids 需要删除的用户点赞主键
     * @return 结果
     */
    @Override
    public int deleteLikesByIds(Long[] ids) {
        return likesMapper.deleteLikesByIds(ids);
    }

    /**
     * 删除用户点赞信息
     *
     * @param id 用户点赞主键
     * @return 结果
     */
    @Override
    public int deleteLikesById(Long id) {
        return likesMapper.deleteLikesById(id);
    }

    @Override
    public Likes getOne(Long userId, Long articleId, Long videosId) {
        Likes l = new Likes();
        l.setUserId(userId);
        l.setArticleId(articleId);
        l.setVideosId(videosId);
        return likesMapper.selectLike(l);
    }

}
